package cn.wipace.actionlib.provider;

import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.net.Uri;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.Log;

import cn.wipace.actionlib.UserDbHelper;
import cn.wipace.actionlib.inter.UserInter;

/**
 * Created by Administrator on 2017/5/24.
 */

public class IceProvider extends ContentProvider {

    //常量UriMatcher.NO_MATCH表示不匹配任何路径的返回码,也就是说如果找不到匹配的类型,返回-1
    private static final UriMatcher uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
    private static final String AUTHORITY = UserInter.ICE_AUTHORITY;
    private static final String TABLE_NAME = UserInter.TABLE_NAME;
    private UserDbHelper mDbHelper;

    private static final int SCORCES = 1;
    private static final int SCORCE = 2;

    static {
        //1.指定一个路径的匹配规则
        //如果路径满足content://com.amos.android_db.provider.PersonProvider/persons,返回值就是(ALL_PERSON)=1
        uriMatcher.addURI(AUTHORITY, TABLE_NAME, SCORCES);
        //2.如果路径满足content://com.amos.android_db.provider.PersonProvider/person/3,返回值就是(PERSON)=2
        //#号为通配符
        uriMatcher.addURI(AUTHORITY, TABLE_NAME + "/#", SCORCE);

    }

    @Override
    public boolean onCreate() {
        mDbHelper = UserDbHelper.getInstance(getContext());
        return true;
    }

    @Nullable
    @Override
    public Cursor query(@NonNull Uri uri, @Nullable String[] projection, @Nullable String selection, @Nullable String[] selectionArgs, @Nullable String sortOrder) {
        Cursor cursor = null;
        switch (uriMatcher.match(uri)) {
            case SCORCES:
                cursor = mDbHelper.queryScoreTop3();
                break;
            case SCORCE:
                cursor = mDbHelper.queryScoreTop3();
                break;
            default:
                throw new RuntimeException("出错了!!");
        }
        return cursor;
    }

    @Nullable
    @Override
    public String getType(@NonNull Uri uri) {
        switch (uriMatcher.match(uri)) {
            case SCORCES:
                return "vnd.android.cursor.dir/" + TABLE_NAME;
            case SCORCE:
                return "vnd.android.cursor.item/" + TABLE_NAME;
            default:
                throw new IllegalArgumentException("Unkwon Uri:" + uri.toString());
        }
    }

    @Nullable
    @Override
    public Uri insert(@NonNull Uri uri, @Nullable ContentValues values) {
        Uri insertUri = null;
        long rowid = 0;
        switch (uriMatcher.match(uri)) {
            case SCORCES:
                rowid = mDbHelper.insertScore(values);
                insertUri = ContentUris.withAppendedId(uri, rowid);
                Log.e("kerry", "insert record...values:" + values.toString());
                break;
            default:
                throw new IllegalArgumentException("Unkwon Uri:" + uri.toString());
        }
        return insertUri;
    }

    @Override
    public int delete(@NonNull Uri uri, @Nullable String selection, @Nullable String[] selectionArgs) {
        return 0;
    }

    @Override
    public int update(@NonNull Uri uri, @Nullable ContentValues values, @Nullable String selection, @Nullable String[] selectionArgs) {
        return 0;
    }
}
